package com.soft.base.enums;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class ValueNameStringDeserializer implements ObjectDeserializer {
    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        String value = parser.parseObject(String.class);
        if (null == value) {
            return null;
        }
        for (Object o : ((Class) type).getEnumConstants()) {
            if (((ValueNameEnum) o).getValue().equals(value)) {
                return (T) o;
            }
        }
        return null;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
